package cn.crc.pjblog.pages;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry.Asset;
import org.apache.tapestry.annotations.ApplicationState;
import org.apache.tapestry.annotations.Component;
import org.apache.tapestry.annotations.Inject;
import org.apache.tapestry.annotations.Path;
import org.apache.tapestry.annotations.Persist;
import org.apache.tapestry.corelib.components.Form;
import org.apache.tapestry.corelib.components.TextField;
import org.apache.tapestry.ioc.Messages;

import cn.crc.model.BlogCategory;
import cn.crc.model.BlogContent;
import cn.crc.model.BlogMember;
import cn.crc.model.imp.IPublicDAO;
import cn.crc.pjblog.data.LogIsShowBean;
import cn.crc.pjblog.data.LogWeatherBean;
import cn.crc.pjblog.services.RedirectException;

import cn.crc.spring.SpringBean;

public class BlogPostTwo {
	
	@ApplicationState
	private BlogMember loginBlogMember;	
	private boolean loginBlogMemberExists;
	
	//configrate FCKEditor 
	@Persist("client")
	private String contentMessage;
	
	@Inject 
	@Path("context:js/myFCKeditorConfig.js")
	private Asset myConfig;
	//configrate FCKeditor end
	
	@Inject
	@SpringBean("IPublicDAO")
	private IPublicDAO pDAO;
	
	@Persist("client")
	private String blogTitle;
	
	@Persist("client")
	private String pubTime;
	
	private boolean logComorder;
	private boolean logIsTop;
	private boolean logDisComment;
	
	private String logFrom;	
	private String logFromURL;
	private String logTags;
	
	@Inject
	private Messages message;
	
	@Component
	private Form frmList;
	
	
//	private String logIsShow;
	
//参数设置
	
	private final static String[] logIsShowName={"公开日志","隐藏日志"};
	@Persist("client")
	private LogIsShowBean  logIsShowBean;
	private List<LogIsShowBean> logIsShowBeanList;
	@Persist("client")
	private LogWeatherBean logWeatherBean;
	private List<LogWeatherBean> logWeatherBeanList;
	
	private final static String[] logWeatherName={"晴天","多云","疾风","冰雹","阴天","下雨","阵雨","下雪"};
//接收BlogPostOne页面的BlogCatagory	
	@Persist("client")
	private BlogCategory  preBlogCategory;
	

	
	
	/**
	 * 页面装载函数,做一些初始化操作
	 * 1 读取数据库时间给前台 pubTime component
	 * */
	public void pageLoaded(){
		System.out.println("页面装载函数");
	
		
		
//		this.setPubTime("page loaded");
	}
	
	/**
	 * 表单提交处理函数
	 * 从前台取得用户填写内容处理后提交到数据库
	 * @author doublel
	 * 
	 * */
	public void onActionFromfrmList(){
		String logTitle = this.getBlogTitle();
		String contentMessage = this.getContentMessage();
		String pubTime = this.getPubTime();
		
		
		String logIsShow = this.getLogIsShowBean().getName();
		
		String logWeather = this.getLogWeatherBean().getName();
		boolean logComorder = this.isLogComorder();
		boolean logIsTop = this.isLogIsTop();
		boolean logDiscomment = this.isLogDisComment();
		
		String logFrom = this.getLogFrom();
		String logFromURL = this.getLogFromURL();
		String logTags = this.getLogTags();
		
		BlogContent  bc = new BlogContent(); 
		
		bc.setLogTitle(logTitle);
		bc.setLogContent(contentMessage);
		bc.setLogPosttime(pubTime);
		bc.setLogIsshow(logIsShow == "公开日志");
		bc.setLogWeather(logWeather);
		bc.setLogComorder(logComorder);
		bc.setLogIstop(logIsTop);
		bc.setLogDiscomment(logDiscomment);
		bc.setLogFrom(logFrom);
		bc.setLogFromurl(logFromURL);
		bc.setLogTag(logTags);
		
		
		try {
			this.pDAO.saveOrUpdate(bc);
		} catch (Exception e) {
			// TODO: handle exception
			this.frmList.recordError(e.getMessage());
			
		}	
		
					
		System.out.println("this.getBlogTitle(="+this.getBlogTitle());
		System.out.println("this.getContentMessage()="+this.getContentMessage());
		System.out.println("this.getPubTime()="+this.getPubTime());
		System.out.println("this.getLogIsShow()="+this.logIsShowBean.getName()+"///"+this.logIsShowBean.getId());
		System.out.println("this.logWeatherBean()="+this.logWeatherBean.getName()+"///"+this.logWeatherBean.getId());
		
		
		
	}
	/**
	 * 页面请求脱离当前线程的时候触发当前方法
	 * */
	public void pageAttached(){
		System.out.println("页面辅佐在当前请求上");
		
	}
	public void pageDetached(){
//		System.out.println(" =======pageDetachedMethod==");
		this.setBlogTitle(null);
		this.setContentMessage(null);
		System.out.println("页面离开在当前请求上");
	}
	/***
	 * 页面在renderder之前执行的方法
	 * 在这里进行用户验证如果没有登录系统就
	 * 不允许发表Blog
	 * 
	 * */
	void onActivate(){
		System.out.println("页面激活函数");
//		System.out.println("onActivate=="+ (order++));
//		用户没有登录需要抛出未登录异常		
		if(this.loginBlogMemberExists == false)
		{
			throw new RedirectException("exceptions/InvalidUserException");
			
		}
		this.setPubTime(this.pDAO.getSystemDateTime());
		//初始化参数select
		 final List<LogIsShowBean> listTemp = new ArrayList<LogIsShowBean>() ;
		 final List<LogWeatherBean> listTemp2 = new ArrayList<LogWeatherBean>();
		for(int i=0;i<logIsShowName.length;i++){
			
			listTemp.add(new LogIsShowBean(i,logIsShowName[i]));			
			
		}
		for(int j=0;j<logWeatherName.length;j++){
			listTemp2.add(new LogWeatherBean(j,logWeatherName[j]));
		}
		
		this.setLogIsShowBeanList(listTemp);
		this.setLogWeatherBeanList(listTemp2);
		
		this.setLogFrom(this.message.get("logFromInitialValue"));
		this.setLogFromURL(this.message.get("logFromURLInitialValue"));
	}
	
	/**
	 * 当前页面激活函数，此处用来获得BlogPostOne页面提交过来的参数BlogCategory
	 * 传入到当前页面变量preBlogCategory中
	 * @param BlogCategory bc
	 * @return none 
	 * 此方法暂时不可用没有调试通过
	 * */
	
//	void onActivate(BlogCategory bc){
//		
//		System.out.println("++++++++bc is null==="+ (bc == null));
//		preBlogCategory = bc ;
//	}
//	
	public void onPassivate(){
		System.out.println("页面毁灭函数");
//		return preBlogCategory;
	}
	
/////////////////////////bean properties/////////////////////////////////////	
	public String getContentMessage() {
		return contentMessage;
	}

	public Asset getMyConfig() {
		return myConfig;
	}

	public void setContentMessage(String contentMessage) {
		this.contentMessage = contentMessage;
	}

	public void setMyConfig(Asset myConfig) {
		this.myConfig = myConfig;
	}

	public IPublicDAO getJdbcDAO() {
		return pDAO;
	}

	public String getPubTime() {
		return pubTime;
	}

	public void setJdbcDAO(IPublicDAO jdbcDAO) {
		this.pDAO = jdbcDAO;
	}

	public void setPubTime(String pubTime) {
		this.pubTime = pubTime;
	}


	public String getBlogTitle() {
		return blogTitle;
	}

	public void setBlogTitle(String blogTitle) {
		this.blogTitle = blogTitle;
	}

//	public String getLogIsShow() {
//		return logIsShow;
//	}
//
//	public void setLogIsShow(String logIsShow) {
//		this.logIsShow = logIsShow;
//	}

	public LogIsShowBean getLogIsShowBean() {
		return logIsShowBean;
	}

	public List<LogIsShowBean> getLogIsShowBeanList() {
		return logIsShowBeanList;
	}

	public void setLogIsShowBean(LogIsShowBean logIsShowBean) {
		this.logIsShowBean = logIsShowBean;
	}

	public void setLogIsShowBeanList(List<LogIsShowBean> logIsShowBeanList) {
		this.logIsShowBeanList = logIsShowBeanList;
	}

	/**
	 * @return the logWeatherBean
	 */
	public LogWeatherBean getLogWeatherBean() {
		return logWeatherBean;
	}

	/**
	 * @return the logWeatherBeanList
	 */
	public List<LogWeatherBean> getLogWeatherBeanList() {
		return logWeatherBeanList;
	}

	/**
	 * @param logWeatherBean the logWeatherBean to set
	 */
	public void setLogWeatherBean(LogWeatherBean logWeatherBean) {
		this.logWeatherBean = logWeatherBean;
	}

	/**
	 * @param logWeatherBeanList the logWeatherBeanList to set
	 */
	public void setLogWeatherBeanList(List<LogWeatherBean> logWeatherBeanList) {
		this.logWeatherBeanList = logWeatherBeanList;
	}

	/**
	 * @return the preBlogCategory
	 */
	public BlogCategory getPreBlogCategory() {
		return preBlogCategory;
	}

	/**
	 * @param preBlogCategory the preBlogCategory to set
	 */
	public void setPreBlogCategory(BlogCategory preBlogCategory) {
		this.preBlogCategory = preBlogCategory;
	}

	/**
	 * @return the logFrom
	 */
	public String getLogFrom() {
		return logFrom;
	}

	/**
	 * @param logFrom the logFrom to set
	 */
	public void setLogFrom(String logFrom) {
		this.logFrom = logFrom;
	}

	/**
	 * @param loginBlogMember the loginBlogMember to set
	 */
	public void setLoginBlogMember(BlogMember loginBlogMember) {
		this.loginBlogMember = loginBlogMember;
	}

	/**
	 * @return the logComorder
	 */
	public boolean isLogComorder() {
		return logComorder;
	}

	/**
	 * @return the logDisComment
	 */
	public boolean isLogDisComment() {
		return logDisComment;
	}

	/**
	 * @return the logIsTop
	 */
	public boolean isLogIsTop() {
		return logIsTop;
	}

	/**
	 * @param logComorder the logComorder to set
	 */
	public void setLogComorder(boolean logComorder) {
		this.logComorder = logComorder;
	}

	/**
	 * @param logDisComment the logDisComment to set
	 */
	public void setLogDisComment(boolean logDisComment) {
		this.logDisComment = logDisComment;
	}

	/**
	 * @param logIsTop the logIsTop to set
	 */
	public void setLogIsTop(boolean logIsTop) {
		this.logIsTop = logIsTop;
	}

	/**
	 * @return the logFromURL
	 */
	public String getLogFromURL() {
		return logFromURL;
	}

	/**
	 * @param logFromURL the logFromURL to set
	 */
	public void setLogFromURL(String logFromURL) {
		this.logFromURL = logFromURL;
	}

	/**
	 * @return the logTags
	 */
	public String getLogTags() {
		return logTags;
	}

	/**
	 * @param logTags the logTags to set
	 */
	public void setLogTags(String logTags) {
		this.logTags = logTags;
	}
	
	
}
